/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatlink;

import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author Nguyen Van Thoi
 */
public class RegisterForm extends MyEvent {

    private ObjectOutputStream _oos;
    private ObjectInputStream _ois;
    private JFrame _jRegisterForm;
    private JButton _jbtnExit, _jbtnSubmit;
    private JLabel _jlblUsername, _jlblPassword, _jlblEmail;
    private JTextField _jtxtUsername, _jtxtEmail;
    private JPasswordField _jtxtPassword;
    private JPanel _jPanel;
    private Socket _sck = null;
    private final int PORT = 9999;
    private final String HOST = "localhost";

    public RegisterForm() {
        _jRegisterForm = new JFrame("Register form");
        _jPanel = new JPanel();

        _jbtnExit = new JButton("Exit");
        _jbtnExit.addActionListener(this);
        _jbtnSubmit = new JButton("Submit");
        _jbtnSubmit.addActionListener(this);

        _jlblUsername = new JLabel("Username");
        _jlblPassword = new JLabel("Password");
        _jlblEmail = new JLabel("Email");

        _jtxtUsername = new JTextField(10);
        _jtxtPassword = new JPasswordField(10);
        _jtxtEmail = new JTextField(10);
    }

    void CreateGUI() {
        _jPanel.add(_jlblUsername);
        _jPanel.add(_jtxtUsername);
        _jPanel.add(_jlblPassword);
        _jPanel.add(_jtxtPassword);
        _jPanel.add(_jlblEmail);
        _jPanel.add(_jtxtEmail);
        _jPanel.add(_jbtnSubmit);
        _jPanel.add(_jbtnExit);
        _jRegisterForm.add(_jPanel);
        _jRegisterForm.setVisible(true);
        _jRegisterForm.setSize(160, 220);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Message message = null;
        if (e.getSource().equals(_jbtnExit)) {
            _jRegisterForm.dispose();
            _jRegisterForm.setVisible(false);
        } else if (e.getSource().equals(_jbtnSubmit)) {
            if (_jtxtEmail.getText().isEmpty() || _jtxtPassword.getPassword().length == 0 || _jtxtUsername.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null, "Bạn phải nhập đầy đủ thông tin");
                return;
            }

            try {
                _sck = new Socket(HOST, PORT);
                _oos = new ObjectOutputStream(_sck.getOutputStream());
                message = new Message();
                message.setMessage("Register");
                StreamData.WriteObject(_oos, message);

                _ois = new ObjectInputStream(_sck.getInputStream());
                message = (Message) StreamData.ReadObject(_ois);
                message = null;

                Account accountInfo = new Account();
                accountInfo.setAccountId(_jtxtUsername.getText());
                accountInfo.setPassword(PasswordUtility.getPassword(_jtxtPassword.getPassword()));
                accountInfo.setEmail(_jtxtEmail.getText());
                
                StreamData.WriteObject(_oos, accountInfo);
                message = (Message) _ois.readObject();

                switch (message.getStatus()) {
                    case REGISTER_SUCCESSFULLY:
                        JOptionPane.showMessageDialog(null, message.getMessage() + "\n");
                        _jRegisterForm.dispose();
                        _jRegisterForm.setVisible(false);
                        break;
                    case REGISTER_FAILED:
                        JOptionPane.showMessageDialog(null, message.getMessage() + "\n");
                        break;
                }

            } catch (SocketException socketException) {
                JOptionPane.showMessageDialog(null, "Kết nối với máy chủ thất bại!");
            } catch (Exception exception) {
                exception.printStackTrace();
            }

        }
    }

    @Override
    public void windowClosed(WindowEvent e) {
        System.exit(1);
    }
}
